﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CCWin;
using GroupChat2013.Entity;
using GroupChat2013.Logic;
using GroupChat2013.Common;
using System.Text.RegularExpressions;
using System.IO;

namespace GroupChat2013.Main
{
    public partial class FrmServiceConfig : CCSkinMain
    {
        private ServcieConfig config;

        private ParseConfig<ServcieConfig> parseConfig;

        /// <summary>
        /// 程序运行目录
        /// </summary>
        private static readonly string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConstClass.DataDirName);
         
        public FrmServiceConfig()
        {
            InitializeComponent();

            this.Load += new EventHandler(FrmServiceConfig_Load);
        }

        public FrmServiceConfig(ParseConfig<ServcieConfig> parseConfig)
            : this()
        {
            this.parseConfig = parseConfig;
        }

        void FrmServiceConfig_Load(object sender, EventArgs e)
        {
            if (parseConfig == null)
                parseConfig = GetServcieConfig();

            config = parseConfig.Parse();

            stbTSIP.SkinTxt.Text = config.PropelConfig.IP;
            stbTSPort.SkinTxt.Text = config.PropelConfig.Port;

            stbYWIP.SkinTxt.Text = config.BusinessConfig.IP;
            stbYWPort.SkinTxt.Text = config.BusinessConfig.Port;
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            if (!IsIPAddress(stbTSIP.SkinTxt.Text))
            {
                MessageBoxEx.Show("推送服务器IP格式错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (!IsIPAddress(stbYWIP.SkinTxt.Text))
            {
                MessageBoxEx.Show("业务服务器IP格式错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (!IsIPPort(stbTSPort.SkinTxt.Text))
            {
                MessageBoxEx.Show("推送服务器端口格式错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (!IsIPPort(stbYWPort.SkinTxt.Text))
            {
                MessageBoxEx.Show("业务服务器端口格式错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            config.PropelConfig.IP = stbTSIP.SkinTxt.Text;
            config.PropelConfig.Port = stbTSPort.SkinTxt.Text;
            config.BusinessConfig.IP = stbYWIP.SkinTxt.Text;
            config.BusinessConfig.Port = stbYWPort.SkinTxt.Text;

            if (parseConfig.SaveConfig(config))
            {
                MessageBoxEx.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBoxEx.Show("保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// 判断是否是Ip地址      
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        private bool IsIPAddress(string ip)
        {
            if (string.IsNullOrEmpty(ip) || ip.Length < 7 || ip.Length > 15) return false;
            string regformat = @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$";
            Regex regex = new Regex(regformat, RegexOptions.IgnoreCase);
            return regex.IsMatch(ip);
        }

        /// <summary>
        /// 判断是否是端口号
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        private bool IsIPPort(string port)
        {
            bool isPort = false;
            int portNum;
            isPort = Int32.TryParse(port, out portNum);
            if (isPort && portNum >= 0 && portNum <= 65535)
            {
                isPort = true;
            }
            else
            {
                isPort = false;
            }

            return isPort;
        }

        public static ParseConfig<ServcieConfig> GetServcieConfig()
        {
            return new ParseConfig<ServcieConfig>(configPath,
                     ConstClass.ServiceConfigName,
                     () => { return new ServcieConfig(); });
        }
    }
}
